import java.util.HashSet;

public class L349 {
    public static void main(String[] args) {
        int[] nums1 = {1, 2, 2, 1};
        int[] nums2 = {2, 2};

        new Solution().intersection(nums1, nums2);
    }


    static class Solution {
        public int[] intersection(int[] nums1, int[] nums2) {
            HashSet<Integer> set1 = new HashSet<>();
            HashSet<Integer> set2 = new HashSet<>();

            for (int j : nums1) {
                set1.add(j);
            }

            for (int i : nums2) {
                if (set1.contains(i)) {
                    set2.add(i);
                }
            }

            int[] ints = new int[set2.size()];
            int j = 0;
            for (Integer integer : set2) {
                ints[j++] = integer;
            }
            return ints;

//            return set2.stream().mapToInt(x -> x).toArray();

        }
    }
}
